package com.shop.om.sysUser.service.impl;

import com.shop.om.enums.Constant;
import com.shop.om.model.sysUser.SysMenuEntity;
import com.shop.om.sysUser.dao.SysMenuDao;
import com.shop.om.sysUser.service.SysMenuService;
import com.shop.om.sysUser.service.SysUserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author cjt
 * @version 1.0.0
 * @description
 * @date 2020/7/14  10:53
 * @Version v1.0
 **/
@Service
public class SysMenuServiceImpl implements SysMenuService {
        @Resource
        private SysMenuDao sysMenuDao;

        @Resource
        private SysUserService sysUserService;




        @Override
        public List<SysMenuEntity> getUserMenuList(Long userId) {
                if (userId == 1){
                        return getAllMenuList(null);
                }

                //用户菜单列表
                List<Long> menuIdList = sysUserService.queryAllMenuId(userId);
                return getAllMenuList(menuIdList);
        }

        @Override
        public SysMenuEntity queryObject(Long menuId) {
                return null;
        }

        @Override
        public List<SysMenuEntity> queryList(Map<String, Object> map) {
                return null;
        }

        @Override
        public int queryTotal(Map<String, Object> map) {
                return 0;
        }

        @Override
        public void save(SysMenuEntity menu) {

        }

        @Override
        public void update(SysMenuEntity menu) {

        }

        @Override
        public void deleteBatch(Long[] menuIds) {

        }

        @Override
        public List<SysMenuEntity> queryUserList(Long userId) {
                return null;
        }

        private List<SysMenuEntity> getAllMenuList(List<Long> menuIdList) {
                //查询根菜单列表
                List<SysMenuEntity> menuList = queryListParentId(0L, menuIdList);
                //递归获取子菜单
                getMenuTreeList(menuList, menuIdList);

                return menuList;
        }

        /**
         * 递归
         */
        private List<SysMenuEntity> getMenuTreeList(List<SysMenuEntity> menuList, List<Long> menuIdList){
                List<SysMenuEntity> subMenuList = new ArrayList<SysMenuEntity>();

                for(SysMenuEntity entity : menuList){
                        if(entity.getType() == Constant.MenuType.CATALOG.getValue()){//目录
                                entity.setList(getMenuTreeList(queryListParentId(entity.getMenuId(), menuIdList), menuIdList));
                        }
                        subMenuList.add(entity);
                }

                return subMenuList;
        }

        @Override
        public List<SysMenuEntity> queryListParentId(Long parentId, List<Long> menuIdList) {
                List<SysMenuEntity> menuList = sysMenuDao.queryListParentId(parentId);
                if(menuIdList == null){
                        return menuList;
                }

                List<SysMenuEntity> userMenuList = new ArrayList<>();
                for(SysMenuEntity menu : menuList){
                        if(menuIdList.contains(menu.getMenuId())){
                                userMenuList.add(menu);
                        }
                }
                return userMenuList;
        }

        @Override
        public List<SysMenuEntity> queryNotButtonList() {
                return null;
        }



}
